﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GeoFly
{
    public class TiessenInter:SpatialInterpolate
    {
        /// <summary>
        /// 得到指定日期的栅格图层插值结果
        /// </summary>
        /// <param name="date">指定的日期</param>
        public GridLayer SpatialGridOut(DateTime date)
        {
            if (this.pStationinfo == null || this.pMeteoData == null)
                return null;

            //栅格数据结构初始化
            m_gridLayer = HydroSimulate.g_GridLayerPara.g_DemLayer.AttributesCopy();

            ProgressBar bar = new ProgressBar();
            bar.Show();
            bar.Text = "正在内插栅格数据";
            if (this.m_CoordType == CoordType.UTM_Coord)
            {
                this.PrepareFromUTM(date);
            }
            else
            {
                this.PrepareFromLongLa(date);
            }
           
            if (X.Length != Y.Length || X.Length != Z.Length)
                throw new Exception("提供的样本数据有误");

            for (int row = 0; row < m_gridLayer.rowCount; row++)		//开始新一日的空间插值计算
            {
                for (int col = 0; col < m_gridLayer.colCount; col++)
                {
                    double value = -9999;
                    //获取当前计算栅格中心坐标
                    //获取当前计算栅格的坐标
                    LPoint p = m_gridLayer.CellPosition(row, col);

                    double CurrentX = p.X;
                    double CurrentY = p.Y;
                    double minDist = 1e20;
                    double minDistValue = 0;
                    for (int i = 0; i < pStationinfo.Count; i++)
                    {
                        double distX = X[i] - CurrentX;
                        double distY = Y[i] - CurrentY;
                        double dist = Math.Sqrt(distX * distX + distY * distY);
                        if (dist < minDist)
                        {
                            minDist = dist;
                            minDistValue = Z[i];
                        }
                    }
                    this.m_gridLayer[row, col] = minDistValue;
                }
            }
            
            //图层中间过程输出
            bar.Close();
            return this.m_gridLayer;
        }
    }
}
